package dk.zpon.andnoise;

import dk.zpon.andnoise.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class Playlist extends ListActivity implements ListView.OnScrollListener {

	Connection mCon;
	ListAdapter mListView;
	private PlaylistAdapter mAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);
		
		mCon = Connection.getInstance();
		
		mAdapter = new PlaylistAdapter(this);
		setListAdapter(mAdapter);
        getListView().setOnScrollListener(this);
        
		mListView = this.getListAdapter();
		this.getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
		{

			@Override
			public boolean onItemLongClick(AdapterView<?> av, View v,
					int position, long id) {
				if(mCon.getUsertype() == Usertype.admin)
				{
					showDialog(((EntryHolder)v.getTag()).getPosition());
				}
				return true;
			}
			
		});
		
	}
	
	protected Dialog onCreateDialog(final int position)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Do you want to remove this track?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                mCon.playlistDelTrack(position);
		                
		                ((PlaylistAdapter)getListAdapter()).removeItem(position);
		                updateListView();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		
		return alert;
	}
	
	void updateListView()
	{
		setListAdapter(mListView);
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
	}
	
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            mAdapter.setBusy(false);

            int count = view.getChildCount();
            for (int i=0; i<count; i++) {
                View v = view.getChildAt(i);
                EntryHolder entry = (EntryHolder) v.getTag();
                if(!entry.dataIsSet())
                {
	                entry.setData(mCon.playlistGetInfo(entry.getPosition()));
	                entry.setText();
                }
            }
            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
        	mAdapter.setBusy(true);
            break;
        case OnScrollListener.SCROLL_STATE_FLING:
            mAdapter.setBusy(true);
            break;
        }
    }

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
	
}

